from widgets.scriptWidgets.ScriptWidget import ScriptWidget
from enums.WidgetType import WidgetType
from PySide6.QtWidgets import QLabel, QComboBox, QPushButton
from ..ScriptInput import ScriptInput
from enums.ZhButtonType import ZhButtonType
from enums.ButtonType import ButtonType
from util.Mouse import MouseUtil
from ..ScriptTimeInput import ScriptTimeInput


class LongPressText(ScriptWidget):
    def __init__(self, initType=None):
        super().__init__(WidgetType.LONG_PRESS_TEXT)
        self.initType = initType

        self.labTitle = QLabel(self)
        self.labTitle.setText('长按文字:')

        self.labTitle.setFixedSize(55, self.lineHeight)
        self.addWidget(self.labTitle)

        self.newLineSpaceWidth = self.labTitle.width() + self.intervalX

        # x
        self.inputX = ScriptInput(self, 'x')
        self.inputX.text = 0
        self.addWidget(self.inputX)

        # y
        self.inputY = ScriptInput(self, 'y')
        self.inputY.text = 0
        self.addWidget(self.inputY)

        # buttonType
        self.comButtonType = QComboBox(self)
        self.comButtonType.addItems(ZhButtonType.values())
        self.comButtonType.setCurrentText(ZhButtonType.values()[0])
        self.addWidget(self.comButtonType)

        if initType == 'edit' or initType == 'create':
            self.addOperate()

        # 宽
        self.inputWidth = ScriptInput(self, '宽')
        self.inputWidth.text = 0
        self.addWidget(self.inputWidth)

        # 高
        self.inputHeight = ScriptInput(self, '高')
        self.inputHeight.text = 0
        self.addWidget(self.inputHeight)

        self.btnTrack = QPushButton(self)
        self.btnTrack.setText('选择区域')
        self.btnTrack.clicked.connect(self.startClickListener)
        self.addWidget(self.btnTrack)

        # 文字
        self.inputText = ScriptInput(self, '文字', 16, 'string')
        self.addWidget(self.inputText)

        self.inputTime = ScriptTimeInput(self, '时间')
        self.addWidget(self.inputTime)

    def onMouseClick(self, x, y, width, height, button):
        self.inputX.text = x
        self.inputY.text = y
        self.inputWidth.text = width
        self.inputHeight.text = height
        buttonName = button.name.upper()
        self.comButtonType.setCurrentText(ZhButtonType[buttonName].value)

    def startClickListener(self):
        MouseUtil.listenArea(self.onMouseClick)

    @property
    def data(self):
        zhType = ZhButtonType(self.comButtonType.currentText())
        buttonType = ButtonType[zhType.name].value
        return {
            'type': self.type.value,
            'x': int(self.inputX.text),
            'y': int(self.inputY.text),
            'width': int(self.inputWidth.text),
            'height': int(self.inputHeight.text),
            'button': buttonType,
            'text': self.inputText.text,
            'time': int(self.inputTime.text),
            'enable': self.switchEnable.isChecked()
        }

    @data.setter
    def data(self, data):
        self.setData(data)
        self.inputX.text = str(data['x'])
        self.inputY.text = str(data['y'])
        self.inputWidth.text = str(data['width'])
        self.inputHeight.text = str(data['height'])
        self.comButtonType.setCurrentText(ZhButtonType[data['button'].upper()].value)
        self.inputText.text = data['text']
        self.inputTime.text = str(data['time'])
